package com.yqkj.zysoft.upms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.easy.common.db.mybatisplus.core.util.PageConvertTool;
import com.project.easy.common.framework.web.entity.dto.SysParamDto;
import  com.project.easy.common.util.collection.CollectionUtil;
import  com.project.easy.common.util.constants.CommonConstant;
import  com.project.easy.common.util.dto.PageResDto;
import  com.project.easy.common.util.exception.business.BusinessException;
import  com.project.easy.common.util.string.StringUtil;
import com.yqkj.zysoft.upms.entity.FrameworkGroup;
import com.yqkj.zysoft.upms.entity.FrameworkGroupAuth;
import com.yqkj.zysoft.upms.entity.dto.FrameWorkGroupAddRequest;
import com.yqkj.zysoft.upms.entity.dto.FrameWorkGroupUpdateRequest;
import com.yqkj.zysoft.upms.entity.dto.PageGroupRequest;
import com.yqkj.zysoft.upms.mapper.FrameworkGroupMapper;
import com.yqkj.zysoft.upms.service.IFrameworkGroupAuthService;
import com.yqkj.zysoft.upms.service.IFrameworkGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * <p>
 * 组 服务实现类
 * </p>
 *
 * @author GreatorMan.cool
 * @since 2022-08-10
 */
@Service
public class FrameworkGroupServiceImpl extends ServiceImpl<FrameworkGroupMapper, FrameworkGroup> implements IFrameworkGroupService {


    @Autowired
    private IFrameworkGroupAuthService frameworkGroupAuthService;

    /**
     * 获取权限集合
     *
     * @param groupId ID
     * @return 返回功能集合
     */
    @Override
    public String getGroupConfigAuth(Long groupId) {
        if (Objects.isNull(groupId)) {
            throw  new BusinessException("组ID不能为空!");
        }
        QueryWrapper<FrameworkGroupAuth> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("group_id", groupId);
        List<FrameworkGroupAuth> frameworkRoleAuths = frameworkGroupAuthService.list(queryWrapper);
        return  CollectionUtil.convertToStringStr(CollectionUtil.convertToList(frameworkRoleAuths, FrameworkGroupAuth::getAuthInnerCode));
    }

    /**
     * 更新权限功能
     *
     * @param authCodes 权限ID
     * @param groupId   组ID
     * @return true 成功
     */
    @Override
    @Transactional
    public Boolean updateGroupConfigAuth(String authCodes, Long groupId) {
        if (Objects.isNull(groupId)) {
            throw  new BusinessException("用户组ID编码不能为空!");
        }
        QueryWrapper<FrameworkGroupAuth> deleteWrapper = new QueryWrapper<>();
        deleteWrapper.eq("group_id", groupId);
        frameworkGroupAuthService.remove(deleteWrapper);
        if (StringUtil.isNotBlank(authCodes)) {
            String[] split = authCodes.split(",");
            List<FrameworkGroupAuth> frameworkAuthFunctions = new ArrayList<>(CollectionUtil.listSize(split));
            Set<String> authCodeMap = new HashSet<>(CollectionUtil.listSize(split));

            for (String auth : split) {
                if (StringUtil.isBlank(auth)) {
                    continue;
                }
                if (authCodeMap.contains(auth)) {
                    continue;
                }
                authCodeMap.add(auth);
                FrameworkGroupAuth frameworkGroupAuth = new FrameworkGroupAuth();
                frameworkGroupAuth.setAuthInnerCode(auth);
                frameworkGroupAuth.setGroupId(groupId);
                frameworkAuthFunctions.add(frameworkGroupAuth);
            }
            if (CollectionUtil.isNotNull(frameworkAuthFunctions)) {
                this.frameworkGroupAuthService.saveBatch(frameworkAuthFunctions);
            }
        }
        return Boolean.TRUE;
    }

    /**
     * 新增
     *
     * @param frameWorkGroupAddRequest 参数
     * @return 返回 true 成功 false 失败
     */
    @Override
    public Long add(FrameWorkGroupAddRequest frameWorkGroupAddRequest) {
        Boolean aBoolean = this.verifyCode(frameWorkGroupAddRequest.getGroupCode(), "", null);
        if (aBoolean) {
            throw  new BusinessException("组编码不能重复");
        }
        FrameworkGroup frameworkGroup = new FrameworkGroup();
        frameworkGroup.setGroupCode(frameWorkGroupAddRequest.getGroupCode());
        frameworkGroup.setGroupName(frameWorkGroupAddRequest.getGroupName());
        boolean save = this.save(frameworkGroup);
        if (save) {
            return frameworkGroup.getId();
        }
        throw  new BusinessException("存储失败");
    }

    /**
     * 更新数据
     *
     * @param frameWorkGroupUpdateRequest 参数
     * @return true 成功 false 失败
     */
    @Override
    public Boolean updateObject(FrameWorkGroupUpdateRequest frameWorkGroupUpdateRequest) {
        FrameworkGroup frameworkGroup = this.getById(frameWorkGroupUpdateRequest.getId());
        if (Objects.isNull(frameworkGroup)) {
            throw  new BusinessException("ID非法");
        }
        Boolean aBoolean = verifyCode(frameWorkGroupUpdateRequest.getGroupCode(), "", frameWorkGroupUpdateRequest.getId());
        if (aBoolean) {
            throw  new BusinessException("修改用户组编码不能重复");
        }
        frameworkGroup.setGroupCode(frameWorkGroupUpdateRequest.getGroupCode());
        frameworkGroup.setGroupName(frameWorkGroupUpdateRequest.getGroupName());
        return  this.updateById(frameworkGroup);
    }

    /**
     * 更新数据
     *
     * @param code    编码
     * @param sysCode 系统编码
     * @return true 成功 false 失败
     */
    @Override
    public SysParamDto queryByCode(String code, String sysCode) {
        return null;
    }

    /**
     * 分页
     *
     * @param groupRequest 分页查询条件
     * @return 返回数据
     */
    @Override
    public PageResDto<FrameworkGroup> pageList(PageGroupRequest groupRequest) {
        Page<FrameworkGroup> page = new Page<>(groupRequest.getPageNum(), groupRequest.getPageSize());
        QueryWrapper<FrameworkGroup> queryWrapper = new QueryWrapper<>();
        if (StringUtil.isNotBlank(groupRequest.getCode())) {
            queryWrapper.like("group_code", groupRequest.getCode());
        }
        if (StringUtil.isNotBlank(groupRequest.getName())) {
            queryWrapper.eq("group_name", groupRequest.getName());
        }
        queryWrapper.eq("has_del", Boolean.FALSE);

        Page<FrameworkGroup> result  = this.baseMapper.selectPage(page, queryWrapper);
        return PageConvertTool.convert(result);
    }

    /**
     * 验证数据
     *
     * @param frameWorkGroupUpdateRequest 参数
     * @return true 验证成功 false 验证失败
     */
    @Override
    public Boolean verifyCode(FrameWorkGroupUpdateRequest frameWorkGroupUpdateRequest) {
        return  this.verifyCode(frameWorkGroupUpdateRequest.getGroupCode(), "", frameWorkGroupUpdateRequest.getId());
    }
    /**
     * 验证数据
     * @param  sysCode 系统编码
     * @param code 编码
     * @param id ID
     * @return true 验证成功 false 验证失败
     */
    private Boolean verifyCode(String code, String sysCode, Long id) {
        if (StringUtil.isBlank(code)) {
            return Boolean.FALSE;
        }
        FrameworkGroup dto  = this.querByCodeAndSysCode(code, sysCode);
        if (Objects.nonNull(id) && Objects.nonNull(dto)) {
            if (dto.getId().compareTo(id) == 0) {
                return Boolean.FALSE;
            }
        }
        return Objects.nonNull(dto);
    }
    /**
     * 根据编码与系统查询
     * @param code 编码
     * @param sysCode 系统编码
     * @return true 成功 false 失败
     */
    private FrameworkGroup querByCodeAndSysCode(String code, String sysCode) {
        if (StringUtil.isBlank(sysCode)) {
            sysCode = CommonConstant.DEFAULT_SYS_CODE;
        }
        QueryWrapper<FrameworkGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("group_code", code);
        queryWrapper.eq("has_del", Boolean.FALSE);
        return this.getOne(queryWrapper);
    }
    /**
     * 启用
     *
     * @param id ID
     * @return true 启用成功 false 启用失败
     */
    @Override
    public Boolean able(Long id) {
        if (Objects.isNull(id)) {
            throw new BusinessException("参数不能为空");
        }
        FrameworkGroup frameworkGroup = this.baseMapper.selectById(id);
        if (Objects.isNull(frameworkGroup)) {
            throw  new BusinessException("参数非法!");
        }
        if (frameworkGroup.getStatus()) {
            throw  new BusinessException("启用不能再次启用!");
        }
        frameworkGroup.setStatus(Boolean.TRUE);
        this.baseMapper.updateById(frameworkGroup);
        return Boolean.TRUE;
    }

    /**
     * 启用
     *
     * @param id ID
     * @return true 启用成功 false 启用失败
     */
    @Override
    public Boolean disAble(Long id) {
        if (Objects.isNull(id)) {
            throw new BusinessException("参数不能为空");
        }
        FrameworkGroup frameworkGroup = this.baseMapper.selectById(id);
        if (Objects.isNull(frameworkGroup)) {
            throw  new BusinessException("参数非法!");
        }
        if (!frameworkGroup.getStatus()) {
            throw  new BusinessException("禁用不能再次禁用!");
        }
        frameworkGroup.setStatus(Boolean.FALSE);
        this.baseMapper.updateById(frameworkGroup);
        return Boolean.TRUE;
    }
}
